package com.hcmus.arLocation;

import java.util.ArrayList;
import java.util.List;
import java.util.Map;

import android.content.Context;
import android.content.Intent;
import android.content.res.Resources.NotFoundException;
import android.graphics.Canvas;
import android.graphics.Color;
import android.graphics.drawable.Drawable;
import android.location.Location;
import android.os.Bundle;
import android.util.AttributeSet;
import android.view.KeyEvent;
import android.view.MotionEvent;
import android.widget.Toast;

import com.google.android.maps.GeoPoint;
import com.google.android.maps.ItemizedOverlay;
import com.google.android.maps.MapActivity;
import com.google.android.maps.MapView;
import com.google.android.maps.Overlay;
import com.google.android.maps.OverlayItem;
import com.hcmus.armobile.R;

public class GeoMappingMarker extends MapActivity{
	private MapView map = null;
	//next two variables are part of a test for longPress event
	private long lastTouchTimeDown = -1;
	private long lastTouchTimeUp = -1;
	
	@Override
	public void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);
		setContentView(R.layout.geomap);
		List<Marker> markers = ARData.getMarkers();
		Toast.makeText(getApplicationContext(),"size list marker on GeoMappingMarker : " + markers.size(),10).show();
		try {
			map = (MapView) findViewById(R.id.mapview);
			map.getController().setZoom(14); //range 1..21	
			map.setBuiltInZoomControls(true);
			Location myCurrLocation = ARData.getCurrentLocation();
			int latitude = (int) (myCurrLocation.getLatitude() * 1e6);
		    int longitude = (int) (myCurrLocation.getLongitude() * 1e6);
			map.getController().setCenter(new GeoPoint(latitude, longitude));
			map.getController().animateTo(new GeoPoint(latitude, longitude));
			Drawable marker = getResources().getDrawable(R.drawable.icon_location);
			marker.setBounds(0, 0, 
							 marker.getIntrinsicWidth(),
							 marker.getIntrinsicHeight());
			Drawable markerMyLocation = getResources().getDrawable(R.drawable.icon_mylocation);
			Marker myLocationMarker = new Marker("My Location","","","","",myCurrLocation.getLatitude(), myCurrLocation.getLongitude(), myCurrLocation.getAltitude(), Color.WHITE);
			ArrayList<Marker> markersMyLocation = new ArrayList<Marker>();
			markersMyLocation.add(myLocationMarker);
			map.getOverlays().add(new SitesOverlay(markerMyLocation,markersMyLocation));
			map.getOverlays().add(new SitesOverlay(marker,markers));
			map.setSatellite(false);
		} catch (NotFoundException e) {
			Toast.makeText(getApplicationContext(), e.getMessage(), 1)
					.show();
		}
	}// onCreate
	
	@Override
	public boolean onKeyDown(int keyCode, KeyEvent event) {
		if (keyCode == KeyEvent.KEYCODE_S) {
			map.setSatellite(!map.isSatellite());
			return (true);
		} 
		return (super.onKeyDown(keyCode, event));
	}



	// ////////////////////////////////////////////////////////////////////////////
		private class SitesOverlay extends ItemizedOverlay<OverlayItem> {

			private List<OverlayItem> items = new ArrayList<OverlayItem>();
			private Drawable marker = null;

			public SitesOverlay(Drawable marker, List<Marker> markers) {
				super(marker);
				this.marker = marker;
				for (Marker mk : markers) {
					int latitude = (int) (mk.physicalLocation.getLatitude() * 1e6);
					int longitude = (int) (mk.physicalLocation.getLongitude() * 1e6);
					String tag = String.valueOf(mk.physicalLocation.getLongitude())
							+"#"+String.valueOf(mk.physicalLocation.getLatitude())
							+"#"+String.valueOf(mk.physicalLocation.getAltitude())
							+"#"+mk.getAddressShopping()
							+"#"+mk.getpromotionShopping()
							+"#"+mk.getIDChainShopping()
							+"#"+mk.getChainShoppingName();
					OverlayItem ovlitem = new OverlayItem(new GeoPoint(latitude, longitude), mk
							.getName(),tag);
					items.add(ovlitem);
				}

				populate();
			}

			@Override
			protected OverlayItem createItem(int i) {
				return (items.get(i));
			}

			@Override
			public void draw(Canvas canvas, MapView mapView, boolean shadow) {
				super.draw(canvas, mapView, shadow);

				boundCenterBottom(marker);
			}

			@Override
			protected boolean onTap(int i) {
				// if time Difference between lastTouchTimeUp & lastTouchTimeDown
				// is:
				// > 1500 millisec. it was a LONG TAP
				// < 1500 just a NORMAL tap
				// on LONG TAPs we may want to show a dialog box with additional
				// data about item i-th such as pictures, links to web-sites, ???,
				// etc.
				// ---------------------------------------------------------------------
//				String text = "NORMAL TAP";
//				long pressTotalTime = lastTouchTimeUp - lastTouchTimeDown;
//				if (pressTotalTime > 1500) {
//					text = "LONG TAP";
//				}
//
//				Toast.makeText(
//						getApplicationContext(),
//						text + " " + pressTotalTime + " msec.\n"
//								+ items.get(i).getTitle() + "\n"
//								+ items.get(i).getSnippet(), Toast.LENGTH_SHORT)
//						.show();
				if(items.get(i).getTitle() == "My Location"){
					Toast.makeText(getApplicationContext(),items.get(i).getTitle() + "\n", Toast.LENGTH_SHORT).show();
					return true;
				}
				String tag = items.get(i).getSnippet();
				String[] arrayTag = tag.split("#");
				String lng = arrayTag[0];
				String lat = arrayTag[1];
				String alt = arrayTag[2];
				String address = arrayTag[3];
				String promotion = arrayTag[4];
				String idChainShop = arrayTag[5];
				String chainShopName = arrayTag[6];
				Intent in = new Intent(GeoMappingMarker.this, DetailLocation.class);
				in.putExtra("location_name_to",String.valueOf(items.get(i).getTitle()));
				in.putExtra("location_address_to",address);
				in.putExtra("location_promotion_to",promotion);
				in.putExtra("location_Location_type_to", idChainShop);
				in.putExtra("location_ChainShopName_to", chainShopName);
				in.putExtra("location_lon_to",lng);
				in.putExtra("location_lat_to",lat);
				startActivity(in);
				return (true);
			}

			// TODO implement longPress actions (such as dialog box showing
			// pictures, links, ???, of selected point.
			@Override
			public boolean onTouchEvent(MotionEvent event, MapView mapView) {
				// remember the initial time the user touches the screen
				if (event.getAction() == MotionEvent.ACTION_DOWN) {
					lastTouchTimeDown = event.getDownTime();
					lastTouchTimeDown = System.currentTimeMillis();
				}
				if (event.getAction() == MotionEvent.ACTION_UP) {
					lastTouchTimeUp = System.currentTimeMillis();
				}
				return super.onTouchEvent(event, mapView);
			}

			@Override
			public int size() {
				return (items.size());
			}
		}// SitesOverlay



		@Override
		protected boolean isRouteDisplayed() {
			// TODO Auto-generated method stub
			return false;
		}
}
